//
//  IMPSearchBar.swift
//  MySina
//
//  Created by imppppp on 16/5/20.
//  Copyright © 2016年 IMP. All rights reserved.
//

import UIKit

class IMPSearchBar: UIView {
    
    @IBOutlet weak var CancleBtn: UIButton!
    @IBOutlet weak var trailingCons: NSLayoutConstraint!
    @IBOutlet weak var searchTF: UITextField!
    
    class func loadSearchBar() -> IMPSearchBar{
        
        let searchBar = NSBundle.mainBundle().loadNibNamed("IMPSearchBar", owner: self, options: nil).last as! IMPSearchBar
        
        return searchBar
    }
    
    override func awakeFromNib() {
        
        // 修改搜索框的宽度
        self.bounds.size.width = ScreenWidth
        self.bringSubviewToFront(self.searchTF)
        // 添加左侧的图片
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        // 设置显示模式,默认是不显示
        searchTF.leftViewMode = .Always
        // 位置优化
        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        searchTF.leftView?.contentMode = .Center
        
//        searchTF.layer.cornerRadius = 20
//        searchTF.clipsToBounds = true
//        searchTF.layer.borderWidth = 2
//        searchTF.layer.borderColor = UIColor.orangeColor().CGColor
//        searchTF.background = UIImage(named: "searchbar_textfield_background")
        
    }
    
    
    @IBAction func searchTF(sender: UITextField) {
        
        self.trailingCons.constant = self.CancleBtn.bounds.width
        
        UIView .animateWithDuration(0.25) {
            
            self.layoutIfNeeded()
            
        }
        
    }
    @IBAction func cancleBtnClick(sender: UIButton) {
        
        self.trailingCons.constant = 0
        UIView .animateWithDuration(0.25) {
            
            self.layoutIfNeeded()
            
        }
        // 取消输入框的第一响应者
        self.endEditing(true)
        
    }
}
